package com.barbeque.mall.controller.wx;

import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.user.MarketUser;
import com.barbeque.mall.bean.wx.cart.*;
import com.barbeque.mall.service.wx.CartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author ycc
 * @date 2022/07/18 19:48:26
 */
@RestController
@RequestMapping("wx/cart")
public class WxCartController {

    @Autowired
    CartService cartService;

    //http://localhost:8083/wx/cart/index
    @GetMapping("index")
    public BaseRespVo index(){
        IndexOfCartVo indexVo = cartService.index();
        return BaseRespVo.ok(indexVo);
    }

    //http://localhost:8083/wx/cart/checked
    @PostMapping("checked")
    public BaseRespVo checked(@RequestBody CheckedOfCartBo checkedBo){
        IndexOfCartVo indexVo = cartService.checked(checkedBo);
        return BaseRespVo.ok(indexVo);
    }

    //http://localhost:8083/wx/cart/update
    @PostMapping("update")
    public BaseRespVo update(@RequestBody MarketCart cart){

        int affectedRows = cartService.update(cart);

        if (affectedRows == 0) {
            return BaseRespVo.unAuthc();
        }

        return BaseRespVo.ok("成功");
    }

    //http://localhost:8083/wx/cart/delete
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Map map){
        IndexOfCartVo indexVo = cartService.delete(map);
        return BaseRespVo.ok(indexVo);
    }

    //TODO:http://localhost:8083/wx/cart/checkout
    @GetMapping("checkout")
    public BaseRespVo checkout(CheckoutOfCartBo checkoutOfCartVo){
        CheckoutOfCartVo cartVo = cartService.checkout(checkoutOfCartVo);
        return BaseRespVo.ok(cartVo);
    }

    //http://localhost:8083/wx/cart/add
    @PostMapping("add")
    public BaseRespVo add(@RequestBody MarketCart cart){
        long cartGoodsCount = cartService.add(cart);
        if (cartGoodsCount==-1) {
            return BaseRespVo.invalidData();
        }
        return BaseRespVo.ok(cartGoodsCount);
    }

    //TODO:http://localhost:8083/wx/cart/fastadd
    @PostMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody MarketCart cart){
        int cartId = cartService.fastadd(cart);
        return BaseRespVo.ok(cartId);
    }

    //http://localhost:8083/wx/cart/goodscount
    @GetMapping("goodscount")
    public BaseRespVo goodscount(){
        long goodsCount = cartService.goodscount();
        return BaseRespVo.ok(goodsCount);
    }
}
